#/bin/bash

DATE=`date +"%y_%m_%d_%H_%M_%S"`
TMP_FILE=/tmp/test_solution_${DATE}_${RANDOM}

#inputs
INPUT_FILE=$1

DIRECTORY="/home/jaime/Dropbox/Mestrado/Pesquisa/Ursa/sat_solvers/My_GPU_sat/My_GPU_Solver/GPUSATSolver/experiments"
CHECK_SOLUTION_AGAINST_CNF=./check_solution_against_cnf.sh
FILE_NAME=none

TOTAL=0
MATCHED=0
MISMATCHED=0
ERRORS=0


rm $TMP_FILE 2> /dev/null

while read line
do

	if [[ "$line" =~ "File = " ]]
	then
		if [[ "$FILE_NAME" != "none" ]]
		then
			cd $DIRECTORY
			echo "Testing: $FILE_NAME"
			$CHECK_SOLUTION_AGAINST_CNF $FILE_NAME $TMP_FILE

			RETURN=$?
			if [[ $RETURN == 10 ]]
			then
				let MATCHED++
			fi

			if [[ $RETURN == 20 ]]
			then
				let MISMATCHED++
			fi

			if [[ $RETURN != 10 ]] && [[ $RETURN != 20 ]]
			then
				let ERRORS++
			fi

			let TOTAL++


			rm $TMP_FILE
		fi

		FILE_NAME=`echo $line | cut -d " " -f 3`

	fi

	if [[ "$line" == "SAT" ]] || [[ "$line" == "UNSAT" ]] || [[ "$line" == "UNDEF" ]] || [[ "$line" =~ (("-")?[1-9][0-9]* )+"0" ]]
	then
		echo $line >> $TMP_FILE
	fi


done < $INPUT_FILE

if [[ "$FILE_NAME" != "none" ]]
then
	cd $DIRECTORY
	echo "Testing: $FILE_NAME"
	$CHECK_SOLUTION_AGAINST_CNF $FILE_NAME $TMP_FILE

	RETURN=$?
	if [[ $RETURN == 10 ]]
        then
        	let MATCHED++
	fi
	if [[ $RETURN == 20 ]]
        then
        	let MISMATCHED++
        fi

        if [[ $RETURN != 10 ]] && [[ $RETURN != 20 ]]
        then
		let ERRORS++
	fi
	let TOTAL++

fi
rm $TMP_FILE

echo "There were $MATCHED matches, $MISMATCHED mismatches and $ERRORS errors of $TOTAL"
